<template>
  <div class="app-container1 show-back-class">
    <div style="width: 100%;height: 60px;border-bottom: #1C97FF 1px solid;padding:  10px 20px;display: flex;">
      <img style="width: 340px; height: 40px; margin-right: 20px;" src="@/assets/bigScreen/index/LOGO.jpg" alt="">
      <big-tab-Bar />
    </div>
    <div class="content-bottom">


      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--        <el-form-item label="购买方" prop="glStationId">-->
<!--          <el-select v-model="queryParams.glStationId" placeholder="请选择购买方" style="width: 100%"  >-->
<!--            <el-option class="index-option" v-for="dict in  stationInfoList" :key="dict.stationId" v-if="dict.status===1"-->
<!--                       :label="dict.stationName" :value="parseInt(dict.stationId)" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->

        <el-form-item label="购买方" prop="purchaser">
          <el-input
            v-model="queryParams.purchaser"
            placeholder="请输入购买方"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>



        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>


      <el-row :gutter="10" class="mb8" style="display: flex; align-items: center;">
        <div style="flex: 1; display: flex;">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['greenCertificate:certificate:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['greenCertificate:certificate:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['greenCertificate:certificate:remove']"
            >删除</el-button>
          </el-col>
        </div>
        <!-- 链接位置 - 右上角 -->
        <div class="url-container">
          <a href="http://103.120.198.33:21004/pxf-green-out/home" target="_blank" class="url-link">
            绿证交易中心
          </a>
        </div>
      </el-row>

      <el-table v-loading="loading" :height="tableHeight" :data="certificateList" @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column width="55" type="index" label="序号" align="center"/>
        <el-table-column label="项目代码" align="center" prop="projectCode" />

        <el-table-column label="购入日期" align="center" prop="intakeDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.intakeDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>

        <el-table-column label="购买方" align="center" prop="purchaser" />
        <el-table-column label="购买电量" align="center" prop="purchaseElectricity" />
        <el-table-column label="减排二氧化碳" align="center" prop="carbonDioxide" />
        <el-table-column label="减排二氧化硫" align="center" prop="sulfurDioxide" />
        <el-table-column label="减排氮氧化物" align="center" prop="nitricOxide" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['greenCertificate:certificate:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['greenCertificate:certificate:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改绿证管理对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="项目代码" prop="projectCode">
            <el-input v-model="form.projectCode" placeholder="请输入项目代码" />
          </el-form-item>
          <el-form-item label="购入日期" prop="intakeDate">
            <el-date-picker clearable size="small" style="width: 200px"
                            v-model="form.intakeDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择购入日期">
            </el-date-picker>
          </el-form-item>

<!--          <el-form-item label="购买方" prop="glStationId" >-->
<!--            <el-select v-model="form.glStationId" placeholder="请选择类型" style="width: 100%"  >-->
<!--              <el-option v-for="dict in  stationInfoList" :key="dict.stationId" v-if="dict.status===1"-->
<!--                         :label="dict.stationName" :value="parseInt(dict.stationId)" />-->
<!--            </el-select>-->
<!--          </el-form-item>-->

          <el-form-item label="购买方" prop="purchaser">
            <el-input v-model="form.purchaser" placeholder="请输入购买方" />
          </el-form-item>

          <el-form-item label="购买电量" prop="purchaseElectricity">
            <el-input v-model="form.purchaseElectricity" placeholder="请输入购买电量(单位kWh)" />
          </el-form-item>
          <el-form-item label="减排二氧化碳" prop="carbonDioxide">
            <el-input v-model="form.carbonDioxide" placeholder="请输入减排二氧化碳(单位kg)" />
          </el-form-item>
          <el-form-item label="减排二氧化硫" prop="sulfurDioxide">
            <el-input v-model="form.sulfurDioxide" placeholder="请输入减排二氧化硫(单位kg)" />
          </el-form-item>
          <el-form-item label="减排氮氧化物" prop="nitricOxide">
            <el-input v-model="form.nitricOxide" placeholder="请输入减排氮氧化物(单位kg)" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listCertificate, getCertificate, delCertificate, addCertificate, updateCertificate, exportCertificate } from "@/api/carbon/greenCertificate";
import { stationInfoDict} from "@/api/station/stationInfo";
import bigTabBar from '@/views/components/bigTabBar/index'
export default {
  name: "certificate",
  components:{
    bigTabBar
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      stationInfoList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 绿证管理表格数据
      certificateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectCode: null,
        purchaser: null,
        purchaseElectricity: null,
        carbonDioxide: null,
        sulfurDioxide: null,
        nitricOxide: null,
        intakeDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tableHeight: '',
    };
  },
  created() {
    this.tableHeight = window.innerHeight - 350;
    stationInfoDict().then(res=>{
      if(res.data){
        this.stationInfoList=res.data
      }
    });
    this.getList();
  },
  methods: {
    /** 查询绿证管理列表 */
    getList() {
      this.loading = true;
      listCertificate(this.queryParams).then(response => {
        this.certificateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectCode: null,
        purchaser: null,
        purchaseElectricity: null,
        carbonDioxide: null,
        sulfurDioxide: null,
        nitricOxide: null,
        intakeDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加绿证管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCertificate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改绿证管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCertificate(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addCertificate(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除绿证管理编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCertificate(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有绿证管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCertificate(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style lang="scss" scoped>
.app-container1{
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #000D17;
  // background: url("../../assets/bg2.jpg") no-repeat;
  // background-size: 100% 100%;
}
.content-bottom{
  width: 100%;
  height: calc(100% - 60px);
  background-color: #000D17;
  padding: 20px;
}
.pagination-container {
  height: 50px;
}

/* 调整链接容器样式 */
.url-container {
  margin-left: auto; /* 将链接推到右侧 */
  padding: 0 10px;
}

.url-link {
  color: #1C97FF;
  font-size: 14px;
  text-decoration: none;
  display: inline-block;
  padding: 5px 10px;
  border: 1px solid #1C97FF;
  border-radius: 4px;
  transition: all 0.3s;
}

.url-link:hover {
  background-color: rgba(28, 151, 255, 0.1);
  text-decoration: none;
}
</style>
